package com.nus.chat;

import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

import android.util.Log;

public class ChatApis {
	public static String host = "172.17.150.222";
	public static int port = 5222;
	public static String service = "nus.rohit-pc";
	public static String username = "a0080079";
	public static String myname = "a0080079";
	public static String password = "a0080079";

	public static XMPPConnection setupConnection() {

		ConnectionConfiguration connConfig = new ConnectionConfiguration(host,
				port, service);
		XMPPConnection connection = new XMPPConnection(connConfig);

		try {
			connection.connect();
			Log.i("XMPPClient",
					"[SettingsDialog] Connected to " + connection.getHost());
		} catch (Exception ex) {
			Log.e("XMPPClient", "[SettingsDialog] Failed to connect to "
					+ connection.getHost());
			Log.e("XMPPClient", ex.toString());

		}
		try {
			connection.login(username, password);
			Log.i("XMPPClient", "Logged in as " + connection.getUser());
			// Set the status to available
			Presence presence = new Presence(Presence.Type.available);
			connection.sendPacket(presence);

		} catch (XMPPException ex) {
			try {
				connection.getAccountManager()
						.createAccount(username, password);
				connection.login(username, password);
				Log.i("XMPPClient", "Logged in as " + connection.getUser());
				// Set the status to available
				Presence presence = new Presence(Presence.Type.available);
				connection.sendPacket(presence);
			} catch (XMPPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Log.e("XMPPClient", "[SettingsDialog] Failed to log in as "
					+ username);
			Log.e("XMPPClient", ex.toString());

		}
		return connection;
	}
}
